from langchain_community.document_loaders import WebBaseLoader
from langchain_community.vectorstores import FAISS
from langchain_community.embeddings import DashScopeEmbeddings
from langchain_text_splitters import RecursiveCharacterTextSplitter
import os
os.environ["TAVILY_API_KEY"] = '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'
os.environ["DASHSCOPE_API_KEY"] = 'sk-5bc0688c5761427cadb9df012e589136'
from langchain_community.tools.tavily_search import TavilySearchResults
search = TavilySearchResults(max_results=1)


from langchain_community.document_loaders import WebBaseLoader
from langchain_community.vectorstores import FAISS
from langchain_community.embeddings import DashScopeEmbeddings
from langchain_text_splitters import RecursiveCharacterTextSplitter
loader = WebBaseLoader("https://help.aliyun.com/zh/dashscope/product-overview/concepts-and-glossary?spm=a2c4g.11186623.0.0.63955491NXmvJ5")
docs = loader.load()
documents = RecursiveCharacterTextSplitter(
    chunk_size=1000, chunk_overlap=200
).split_documents(docs)
vector = FAISS.from_documents(documents, DashScopeEmbeddings())
retriever = vector.as_retriever()


from langchain.tools.retriever import create_retriever_tool
retriever_tool = create_retriever_tool(
    retriever,
    "search_dashscope_knowledge",
    "当需要搜索灵积模型相关知识的时候必须使用该工具",
)

from langchain_core.tools import tool
@tool
def multiply(first_int: int, second_int: int) -> int:
    """两个数的乘积."""
    return first_int * second_int


# 创建工具列表
toolList = [search, retriever_tool, multiply]

from langchain_community.chat_models.tongyi import ChatTongyi
llm = ChatTongyi(model="qwen-max")
llm_with_tools = llm.bind_tools([search, retriever_tool, multiply])

# 未使用工具
msg = llm.invoke("你好")
print(msg)

# 建议使用工具：search_dashscope_knowledge
msg = llm_with_tools.invoke("灵积模型是什么")
print(msg)

# 建议使用工具：multiply
msg = llm_with_tools.invoke("计算 10 的 5 倍")
print(msg)